angular.module('inspinia').controller('policyTargetAddCtrl',function($scope, $http, $state, $stateParams){

	$scope.vipCategories = [{text:"全部",value:null}, {text:"同旬同类",value:"1"},{text:"同旬不限类",value:"2"},{text:"不限旬不限类",value:"3"}];
	$scope.vipCategoryTypes = [{text:"全部",value:null}, {text:"最高值",value:"1"},{text:"平均值",value:"2"},{text:"最低值",value:"3"}];
	$scope.nonCategories = [{text:"全部",value:null}, {text:"同旬同类",value:"1"},{text:"同旬不限类",value:"2"},{text:"不限旬不限类",value:"3"}];
	$scope.nonCategoryTypes = [{text:"全部",value:null}, {text:"最高值",value:"1"},{text:"平均值",value:"2"},{text:"最低值",value:"3"}];
	$scope.transCategories = [{text:"全部",value:null}, {text:"同旬同类",value:"1"},{text:"同旬不限类",value:"2"},{text:"不限旬不限类",value:"3"}];
	$scope.transCategoryTypes = [{text:"全部",value:null}, {text:"最高值",value:"1"},{text:"平均值",value:"2"},{text:"最低值",value:"3"}];

	$scope.selectMTimes = function(){
		$http.post('policyTarget/selectMTimes', {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
		).success(function(result){
			$scope.mTimes = result.data;
		}).error(function(){
		});
	};
	$scope.selectMTimes();

	$scope.getDetail = function(){
		let id = $stateParams.id;
		if(!id){
			$scope.info = {vipCategory:null,vipCategoryType:null,nonCategory:null,nonCategoryType:null,transCategory:null,transCategoryType:null,mTimeStr:null};
			return;
		}
		$http.get('policyTarget/recordDetial?id='+id).success(
			function (data) {
				$scope.info = data.info;
				$scope.mTimes = data.mTimes;
				angular.forEach($scope.mTimes, function (e) {
					if(e.mTime == $scope.info.mTime){
						e.isChecked = 1;
						$scope.info.mTimeStr = $scope.info.mTime;
					}else {
						e.isChecked = 0;
					}
				});
			});
	}
	$scope.getDetail();



	$scope.changeBox = function(id,isChecked){
		if(isChecked == 1){
			angular.forEach($scope.mTimes, function (e) {
				if(e.id != id){
					e.isChecked = 0;
				}else{
					$scope.info.mTimeStr = e.mTime;
				}
			});
		}else {
			$scope.info.mTimeStr = null;
		}
	}


	$scope.submit = function(){
		//angular.toJson($scope.submitData)
		$scope.submitting = true;
		$http.post("policyTarget/savePolicyTarget",$scope.info)
			.success(function(result){
			if(result.status){
				$scope.notice("新增成功");
				$state.transitionTo('org.policyTarget',null,{reload:true});
			} else {
				$scope.notice(result.msg);
				$scope.submitting = false;
			}
		});
	}



});